package io.github.neo.hhcache.command.list;

import io.github.neo.hhcache.core.Command;
import io.github.neo.hhcache.core.HHCache;
import io.github.neo.hhcache.entry.Reply;

/**
 * @author: hhz
 * @create: 2024/7/10
 **/
public class LpopCommand implements Command {
    @Override
    public String name() {
        return "LPOP";
    }

    @Override
    public Reply<?> exec(HHCache cache, String[] args) {
        String key = getKey(args);
        int count = 1;
        if(args.length > 6) {
            String val = getVal(args);
            count = Integer.parseInt(val);
            return Reply.array(cache.lpop(key, count));
        }
        final String[] lpop = cache.lpop(key, count);
        return Reply.bulkString(lpop == null || lpop.length == 0 ? null : lpop[0]);
    }
}
